using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace RedPointSystem
{
    /// <summary>
    /// 红点检测管理器
    /// </summary>
    public class RedPointCheckMgr
    {
        public delegate bool OnRedpointCheck(RedPointData redPointData);
        
        private static RedPointCheckMgr ms_instance = null;
        private RedPointCheckResultData m_resultData = new RedPointCheckResultData();
        
        private Dictionary<int, OnRedpointCheck> m_checkerDic = new Dictionary<int, OnRedpointCheck>();

        private Dictionary<int, List<RedPoint>> m_allRedPoints = new Dictionary<int, List<RedPoint>>();

        public RedPointCheckMgr()
        {
            
        }

        ~RedPointCheckMgr()
        {
            
        }

        public static RedPointCheckMgr GetInstance()
        {
            if (ms_instance == null)
            {
                ms_instance = new RedPointCheckMgr();
            }

            return ms_instance;
        }

        public void AddRedPoint(int typeId, RedPoint redPoint)
        {
            if (m_allRedPoints.ContainsKey(typeId))
            {
                m_allRedPoints[typeId].Add(redPoint);
            }
            else
            {
                List<RedPoint> points = new List<RedPoint>();
                points.Add(redPoint);
                m_allRedPoints.Add(typeId, points);
            }
        }

        public void RemoveRedPoint(int typeId, RedPoint redPoint)
        {
            if (m_allRedPoints.ContainsKey(typeId))
            {
                if (m_allRedPoints[typeId].Contains(redPoint))
                {
                    m_allRedPoints[typeId].Remove(redPoint);
                }
            }
            /*else
            {
                Debug.LogError("无法找到 " + typeId);
            }*/
        }

        public void CheckAll()
        {
            if (m_allRedPoints == null || m_allRedPoints.Count == 0)
            {
                return;
            }

            foreach (var points in m_allRedPoints.Values)
            {
                foreach (var redPoint in points)
                {
                    redPoint.Check();
                }
            }
        }

        public RedPointCheckResultData Check(RedPoint redPoint, RedPointData redPointData)
        {
            OnRedpointCheck checker = null;
            if (m_checkerDic.TryGetValue(redPointData.redPointTypeId, out checker))
            {
                m_resultData.SetData(checker(redPointData), 0);
            }
            else
            {
                Debug.LogError($"无法找到typeid = {redPointData.redPointTypeId}的检测逻辑，item = {redPoint.name}", redPoint);
                m_resultData.SetData(false, 0);
                return m_resultData;
            }

            if (!m_resultData.isExsit)
            {
                m_resultData.SetData(false, 0);
            }

            return m_resultData;
        }

        /// <summary>
        /// 检测指定typeId
        /// </summary>
        /// <param name="typeId"></param>
        public void CheckByType(int typeId)
        {
            if (m_allRedPoints.ContainsKey(typeId))
            {
                foreach (var redPoint in m_allRedPoints[typeId])
                {
                    redPoint.Check();
                }
            }
        }
        
        /// <summary>
        /// 注册红点检测逻辑
        /// </summary>
        public void Register(int typeId, OnRedpointCheck checkFunc)
        {
            if (this.m_checkerDic.ContainsKey(typeId))
            {
                Debug.LogError($"重复注册检测逻辑，typeid = {typeId}");
            }
            else
            {
                this.m_checkerDic[typeId] = checkFunc;
            }
        }
        
        /// <summary>
        /// 取消红点检测逻辑
        /// </summary>
        public void UnRegister(int typeId)
        {
            if (!this.m_checkerDic.ContainsKey(typeId))
            {
                Debug.LogError($"未找到注册处理逻辑，typeid = {typeId}");
            }
            else
            {
                this.m_checkerDic[typeId] = null;
                this.m_checkerDic.Remove(typeId);
            }
        }

        /// <summary>
        /// 清理所有检测逻辑
        /// </summary>
        public void UnRegisterAll()
        {
            this.m_checkerDic.Clear();
        }
    }
}

